﻿using System;
using System.Collections.Generic;

namespace Central.Linq {

	/// <summary>
	/// Расширение type
	/// </summary>
	public static class TypeExtensions {

		/// <summary>
		/// Метод получения списка допустимых операций фильтрации
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public static IEnumerable<FilterOperator> ApplicableFilterOperators(this Type type) {
			if (type == null)
				return new FilterOperator[] { };
			else if (type == typeof(string))
				return new FilterOperator[]
				{
					FilterOperator.Contains,
					FilterOperator.DoesNotContain,
					FilterOperator.StartsWith,
					FilterOperator.DoesNotStartsWith,
					FilterOperator.EndsWith,
					FilterOperator.DoesNotEndsWith,
					FilterOperator.IsNull,
					FilterOperator.IsNotNull,
					FilterOperator.IsEqualTo,
					FilterOperator.IsNotEqualTo,
					FilterOperator.IsGreaterThan,
					FilterOperator.IsGreaterThanOrEqualTo,
					FilterOperator.IsLessThan,
					FilterOperator.IsLessThanOrEqualTo
				};
			else if (type == typeof(bool))
				return new FilterOperator[]
				{
					FilterOperator.IsEqualTo,
					FilterOperator.IsNotEqualTo,
				};
			else if (type.IsValueType)
				return new FilterOperator[]
				{
					FilterOperator.IsNull,
					FilterOperator.IsNotNull,
					FilterOperator.IsEqualTo,
					FilterOperator.IsNotEqualTo,
					FilterOperator.IsGreaterThan,
					FilterOperator.IsGreaterThanOrEqualTo,
					FilterOperator.IsLessThan,
					FilterOperator.IsLessThanOrEqualTo
				};
			else
				return new FilterOperator[] { };
		}

		/// <summary>
		/// Метод получения списка допустимых операций фильтрации
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public static IEnumerable<AggregateFunction> ApplicableAggregateFunctions(this Type type) {
			if (type == null)
				return new AggregateFunction[]
				{
					AggregateFunction.None
				};
			else if (type == typeof(String) || type == typeof(DateTime) || type == typeof(TimeSpan))
				return new AggregateFunction[]
				{
					AggregateFunction.None,
					AggregateFunction.Min,
					AggregateFunction.Max,
				};
			else if (type != typeof(Boolean) && type.IsValueType)
				return new AggregateFunction[]
				{
					AggregateFunction.None,
					AggregateFunction.Min,
					AggregateFunction.Max,
					AggregateFunction.Sum,
					AggregateFunction.Average
				};
			else
				return new AggregateFunction[]
				{
					AggregateFunction.None
				};
		}
	}
}